package com.junzerg.tooltip.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.junzerg.tooltip.constant.Constant;
import com.junzerg.tooltip.controller.response.Result;
import com.junzerg.tooltip.controller.response.Status;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseController {

    public Result test() {
        Result result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(Status.SUCCESS.getMsg());
        result.setData("佛祖保佑永不宕机！");
        return result;
    }

    /**
     * 返回错误结果模板
     *
     * @param code 状态码
     * @param msg  附带信息
     * @return
     */
    public Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public Result error(Status status) {
        Result result = new Result();
        result.setCode(status.getCode());
        result.setMsg(status.getMsg());
        return result;
    }

    public Result error(String message) {
        Result result = new Result();
        result.setMsg(message);
        result.setCode(10001);
        return result;
    }

    /**
     * 返回成功结果模板
     *
     * @return
     */
    public Result success() {
        Result result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(Status.SUCCESS.getMsg());

        return result;
    }


    /**
     * 返回成功结果模板
     *
     * @param msg:附带信息
     * @return
     */
    public Result success(String msg, Integer code) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);

        return result;
    }


    public Result success(Object data) {
        Result result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(Status.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public <T> Result<T> responseSuccess(T data) {
        Result<T> result = new Result();
        result.setCode(Status.SUCCESS.getCode());
        result.setMsg(Status.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    protected JSONArray list2JsonArray(List<? extends Object> list) {
        JSONArray array = new JSONArray();
        if (list != null && list.size() != 0) {
            JSONObject item = null;
            for (Object obj : list) {
                item = JSON.parseObject(JSON.toJSONString(obj));
                if (item != null) {
                    array.add(item);
                }
            }
        }
        return array;
    }

    public Map<String, Object> checkPageParams(int pageNo, int pageSize) {
        Map<String, Object> result = new HashMap<>(2);
        Status resultEnum = Status.SUCCESS;
        String msg = Status.SUCCESS.getMsg();
        if (pageNo <= 0) {
            resultEnum = Status.REQUEST_PARAMS_NOT_VALID_ERROR;
            msg = MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), Constant.PAGE_NUMBER);
        } else if (pageSize <= 0) {
            resultEnum = Status.REQUEST_PARAMS_NOT_VALID_ERROR;
            msg = MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), Constant.PAGE_SIZE);
        }
        result.put(Constant.STATUS, resultEnum);
        result.put(Constant.MSG, msg);
        return result;
    }

    public <T> Result<T> paramError(String param) {
        Result result = new Result();
        result.setCode(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode());
        result.setMsg("参数有误：【" + param + "】");
        return result;
    }
}
